package com.ttg.web.utils;

import com.ttg.common.enums.AccountDimTypeEnum;
import com.ttg.common.enums.AccountItemTypeEnum;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Description：
 * Author: chenyou
 * Date: 2025/10/9 13:57
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
public class AccountDimTypeUtil {
    //保存账户维度各类计算金额
    private static InheritableThreadLocal<Map<AccountDimTypeEnum, HashMap<String, BigDecimal>>> dimMap = new InheritableThreadLocal<>();

    private AccountDimTypeUtil(){}



    public static void putResult(AccountDimTypeEnum dimType, String subKey, BigDecimal subVal){
        Map<AccountDimTypeEnum, HashMap<String, BigDecimal>> dimMap = getDimMap();
        boolean isNullFlag = false;
        if(dimMap==null || dimMap.isEmpty()){
            dimMap = new HashMap<AccountDimTypeEnum, HashMap<String, BigDecimal>>();
            isNullFlag = true;
        }

        HashMap<String,BigDecimal> subMap = dimMap.get(dimType);
        if(subMap==null){
            subMap = new HashMap<String,BigDecimal>();
            dimMap.put(dimType,subMap);
        }
        subMap.put(subKey,subVal);

        if(isNullFlag){
            setDimMap(dimMap);
        }
    }

    public static BigDecimal getResult(AccountDimTypeEnum dimType, String subKey){
        Map<AccountDimTypeEnum, HashMap<String, BigDecimal>> dimMap = getDimMap();
        if(dimMap==null || dimMap.isEmpty()){
            return new BigDecimal(0);
        }

        HashMap<String,BigDecimal> subMap = dimMap.get(dimType);
        if(subMap==null){
            return new BigDecimal(0);
        }
        return subMap.get(subKey);
    }


    public static Map<AccountDimTypeEnum, HashMap<String, BigDecimal>> getDimMap(){
        return dimMap.get();
    }

    public static void setDimMap(Map<AccountDimTypeEnum, HashMap<String, BigDecimal>> paramap){
        dimMap.set(paramap);
    }

    public static void removeDimMap(){
        dimMap.remove();
    }

}
